<?php

// Utilities.php
// Michael Baker
// 2010.06.25
//
// provides miscellaneous functionality

if ( !defined( "IN_MDM" ) )
  exit;
  
require_once( "all.php" );

function device_responds( $addr )
{
  // Check whether an IP address responds to a simple fsockopen
  
  $errno = 0;
  $errstr = "";
  
  $sock = @fsockopen( $addr, QDB_MYSQL_PORT, $errno, $errstr, SOCK_TIMEOUT );
  $ret = ($sock) ? true : false;
  
  @socket_close($sock);
  
  return $ret;
}

function generate_couple_titles( $couple_array )
{
  // Go through each nodecouple in the array and check if its
  // nodes have titles (this is computationally inefficient!)
  
  global $dbase;  
  $affected = 0;
  
  foreach ( $couple_array as $couple_id )
  {
    $couple = $dbase->get_nodecouple( $couple_id );
    $nodes  = $couple->get_nodes();
    
    $qdb = $couple->get_db_conn();
    $qdb->set_date( get_ymd( time() ) );
    
    foreach ( $nodes as $node )
    {
      $cond = !( $node->get_title() );
      
      if ( $cond )
      {
        $node_addr  = $node->get_address();
        $node_id    = $node->get_id();
        $node_title = $qdb->get_node_title( $node_addr );
        
        $dbase->update_node_title( $node_id, $node_title );
        $affected += 1;
        
      }
    }
    
    $qdb->close();
  }
  
  return $affected;
}

function autodetect_db_flags2( $nc_obj_array )
{
  // Autodetect DB flag for each NodeCouple object in the array.
  
  $flags = array();
  
  foreach ( $nc_obj_array as $nc_obj )
  {
    $nodes = $nc_obj->get_nodes();
    
    $init_node = $nodes[ 0 ];
    $fin_node  = $nodes[ 1 ];
    
    $init_addr = $init_node->get_address();
    $fin_addr  = $fin_node->get_address();
    
    if ( $init_node->qdb_suffices( $fin_addr ) )
      $flag = 0;
    else if ( $fin_node->qdb_suffices( $init_addr ) )
      $flag = 1;
    else
      throw new Exception( "Failed to autodetect nodecouple DB flags: Nodecouple ($init_addr, $fin_addr) lacks a data source" );
    
    $flags[] = $flag;
  }
  
  return $flags;
}

function autodetect_db_flags( $couple_array )
{
  // Autodetect DB flag for each coupleID in the array
  // Overrides any existing DB flags for the couples.
  
  global $dbase;
  $affected = 0;
  
  foreach ( $couple_array as $couple_id )
  {
    $couple = $dbase->get_nodecouple( $couple_id );
    
    $nodes  = $couple->get_nodes();
    
    $init_node = $nodes[ 0 ];
    $fin_node  = $nodes[ 1 ];
    
    $init_addr = $init_node->get_address();
    $fin_addr  = $fin_node->get_address();
    
    if ( $init_node->qdb_suffices( $fin_addr ) )
      $flag = 0;	// use the initial
    else if ( $fin_node->qdb_suffices( $init_addr ) )
      $flag = 1;	// use the final
    else
      throw new Exception( "Failed to autodetect nodecouple DB flags: Nodecouple $couple_id lacks a data source" );
    
    $dbase->update_nodecouple_db_flag( $couple_id, $flag );
    
    $affected += 1;
  }
  
  return $affected;
}
        
function is_ip_address( $str )
{
  $pattern = '/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/';
  return preg_match( $pattern, $str );
}

function induce_duplicates( $ip_arr )
{
  // converts an array of type (a, b, c, d) to type (a, b, b, c, c, d)
  
  $retval = array();
  $n = count( $ip_arr );

  for ( $k = 0; $k < ($n - 1); $k++ )
    array_push( $retval, $ip_arr[ $k ], $ip_arr[ $k + 1 ] );
  
  return $retval;
}

function remove_duplicates( $ip_arr )
{
  $retval = array();

  for ( $k = 0; $k < count( $ip_arr ); $k += 2 )  
    $retval[] = $ip_arr[ $k ];
  
  $retval[] = $ip_arr[ (count($ip_arr) - 1) ];

  return $retval;
}

?>